package com.jw.system.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jw.response.Result;
import com.jw.system.entity.Goods;
import com.jw.system.entity.vo.UserVO;
import com.jw.system.service.GoodsService;
import com.jw.system.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author jw
 * @since 2021-04-20
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 获取商品列表
     * @return
     */
    @ApiOperation(value = "用户列表", notes = "模糊查询用户列表")
    @PostMapping("/getGoodsList")
    public Result getGoodsList(@RequestParam(value = "current", required = false) Integer current,
                              @RequestParam(value = "size", required = false) Integer size,
                               @RequestParam(value = "categorys", required = false) String categorys,
                              @RequestBody Goods goods){
        buildCategorySearch(categorys, goods);
        Page<Goods> goodsList = goodsService.getGoodsList(current, size, goods);
        return Result.ok().data("goodsList",goodsList.getRecords()).data("total",goodsList.getTotal());
    }

    /**
     * 封装物资查询条件
     */
    public void buildCategorySearch(@RequestParam(value = "categorys", required = false) String categorys, Goods goods) {
        if (categorys != null && !"".equals(categorys)) {
            String[] split = categorys.split(",");
            switch (split.length) {
                case 1:
                    goods.setOneCatId(Long.parseLong(split[0]));
                    break;
                case 2:
                    goods.setOneCatId(Long.parseLong(split[0]));
                    goods.setTwoCatId(Long.parseLong(split[1]));
                    break;
            }
        }
    }

}

